package com.pai.service.LeaveTicket;

import com.pai.dao.BaseDao;
import com.pai.dao.LeaveTicket.LeaveTicketDao;
import com.pai.dao.LeaveTicket.LeaveTicketDaoImpl;
import com.pai.entity.LeaveTicket;
import com.pai.util.DateUtil;


import java.sql.Connection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class LeaveTicketServiceImpl implements LeaveTicketService {
    private LeaveTicketDao theDao;  //部门dao

    public LeaveTicketServiceImpl() {
        this.theDao = new LeaveTicketDaoImpl();
    }

    /**
     * 获取列表
     * @return
     */
    @Override
    public List<LeaveTicket> getList() {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<LeaveTicket> resultList = new ArrayList<>();
        try {
            resultList = theDao.getList(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取列表
     * @param claId  班级id
     * @param starDate  请假开始时间
     * @param endDate  请假结束时间
     * @return
     */
    @Override
    public List<LeaveTicket> getList(String claId, Date starDate, Date endDate) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<LeaveTicket> resultList = new ArrayList<>();
        try {
            resultList = theDao.getList(connection, claId, starDate, endDate);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取请假单列表（学生）
     * @param stuId  学生id
     * @return
     */
    @Override
    public List<LeaveTicket> getListStu(String stuId) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<LeaveTicket> resultList = new ArrayList<>();
        try {
            resultList = theDao.getListStu(connection, stuId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取请假单列表（辅导员）
     * @param colId  辅导员id
     * @return
     */
    @Override
    public List<LeaveTicket> getListCol(String colId) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<LeaveTicket> resultList = new ArrayList<>();
        try {
            resultList = theDao.getListCol(connection, colId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取列表
     * @param depType 部门类型： 0：学院 1：学工处 2：班级
     * @return
     */
    @Override
    public List<LeaveTicket> getList(String depType) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<LeaveTicket> resultList = new ArrayList<>();
        try {
            resultList = theDao.getList(connection, depType);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取班级部门列表
     * @param depId  父级部门id
     * @return
     */
    @Override
    public List<LeaveTicket> getDepClassList(String depId) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<LeaveTicket> resultList = new ArrayList<>();
        try {
            resultList = theDao.getDepClassList(connection, depId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取项详情
     * @param id
     * @return
     */
    @Override
    public LeaveTicket getItemDetail(String id) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        LeaveTicket item = new LeaveTicket();
        try {
            item = theDao.getItemDetail(connection, id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return item;
    }

    /**
     * 获取项详情
     * @param depName  部门名称
     * @param depType  部门类型
     * @return
     * @throws Exception
     */
    @Override
    public LeaveTicket getItemDetail(String depName, String depType) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        LeaveTicket item = new LeaveTicket();
        try {
            item = theDao.getItemDetail(connection, depName, depType);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return item;
    }

    /**
     * 新增
     * @param item
     * @return
     */
    @Override
    public boolean add(LeaveTicket item) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            flag = theDao.add(connection, item) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }



    /**
     * 更新
     * @param item
     * @return
     */
    @Override
    public boolean update(LeaveTicket item) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            flag = theDao.update(connection, item) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @Override
    public boolean delete(String id) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            flag = theDao.delete(connection, id) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }

    /**
     * 生成请假单号
     * @param date 日期
     * @return
     */
    @Override
    public String generateLeaveNo(Date date) {

        String year = String.format("%tY", date);

        String month4 = String.format("%tm", date);

        String day = String.format("%te", date);

        System.out.println(year+"-"+month4+"-"+day);
        String leaveDateNo = year + month4 + day;
        int count = 0;
        Connection connection = null;
        connection = BaseDao.getConnection();


        try {
            count = theDao.getListCount(connection, date) ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        count += 1;
        String countStr = count + "";
        while (countStr.length() < 4)
        {
            countStr = "0" + countStr;
        }

        BaseDao.closeResource(connection, null, null);
        return leaveDateNo + countStr;

    }
}
